const { task } = require("hardhat/config")

 
// 测试合约，需要带入一个合约地址的参数 合约地址就是部署合约的时候打印的
//   PS D:\code\web3> npx hardhat test-fundme --addr 0xc5e326a4C247E82962ac9207A5f6014bE9C7F024 --network sepolia
task("test-fundme", "deploy and verify fundme conract")
  .addParam("addr", "The address of the fundme contract")
  .setAction(async (taskArgs, hre) => {
    const fundMeFactory = await ethers.getContractFactory("FundMe")
    const fundMe = fundMeFactory.attach(taskArgs.addr)



    const [firstAccount, secondAccount] = await ethers.getSigners();
    console.log("firstAccount address:", firstAccount.address);

    // 使用第一个账户支付
    let fundTx = await fundMe.fund({ value: ethers.parseEther("0.001") });
    await fundTx.wait();
    console.log("fundTx hash:", fundTx.hash);

    // 查看 fundMe 的余额
    let balanceOneStep = await ethers.provider.getBalance(fundMe.target);
    console.log("fundMe balance:", balanceOneStep);

    // 使用第二个账户支付
    const fundTxWithSecondAccount = await fundMe.connect(secondAccount).fund({ value: ethers.parseEther("0.003") });
    await fundTxWithSecondAccount.wait();
    console.log("fundTx hash:", fundTxWithSecondAccount.hash);

    // 查看 fundMe 的余额
    let balancetowStep = await ethers.provider.getBalance(fundMe.target);
    console.log("fundMe balance:", balancetowStep);

    // 获取账户的贡献金额
    const firstAccountAmount = await fundMe.fundersToAmount(firstAccount.address);
    const secondAccountAmount = await fundMe.fundersToAmount(secondAccount.address);

    console.log("firstAccount.address:", firstAccount.address, "         firstAccountAmount:", firstAccountAmount.toString());
    console.log("secondAccount.address:", secondAccount.address, "         secondAccountAmount:", secondAccountAmount.toString());

  })


module.exports = {}